package terra.model.object.map;

import terra.Config;
import terra.definitions.GroundDef;
import terra.managers.DefManager;
import terra.model.World;
import terra.model.map.Map;
import terra.model.object.GObject;
import terra.model.object.ItemObject;
import terra.model.object.item.Item;
import terra.network.Connections;
import terra.network.packet.server.TileInfo;
import terra.network.packet.server.battle.HitInfo;
import terra.util.Util;

public class GroundObject extends AbstractMapObject {

	private static final long serialVersionUID = -189830065508576094L;

	private int hp;
	private int maxHp;

	private short backgroundId = -1;

	public boolean canMove = false;
	public boolean isLadder = false;

	public GroundObject(AbstractMapObject abstractMapObject, String string) {
		super(abstractMapObject);
	}

	public GroundObject(int x, int y, short tX, short tY, GroundDef def, Map map) {
		super(x, y, tX, tY, map);
		setMaxHp(def.baseHp);
		setId((short) def.id);
		isLadder = def.getBoolProperty("isLadder");
		if (isLadder)
			canMove = true;
		else
			canMove = def.getBoolProperty("canMove");
	}

	@Override
	public byte getType() {
		return TYPE_GROUND;
	}

	@Override
	public boolean canMove() {
		return canMove;
	}

	public int getHp() {
		return hp;
	}

	public void setHp(int life) {
		this.hp = life;
	}

	@Override
	public String getBackgroundImage() {
		return getDef().image + "_back";
	}

	public String getResourceImage() {
		return getDef().resourceImage;
	}

	public void hit(int damage) {
		setHp(getHp() - damage);
		if (getHp() <= 0) {
			AbstractMapObject obj = new MapObject(this);
			// FIXME hardcode for tree blocks
			if (getId() >= 21 && getId() < 25) {
				obj.setId(getBackgroundId());
			} else {
				listenChanges();
			}
			getMap().getMap()[getTileX()][getTileY()] = obj;
			obj.tileChanged();
			World.getInstance().groundListener.checkNewGround(obj);
			if (getListeners() != null)
				getListeners().clear();
			if (getDef().itemId != -2) {
				ItemObject item = new ItemObject();
				item.setX(getX());
				item.setY(getY() + Config.tileSize / 2 - Config.tileSize / 4);
				item.setWidth(Config.tileSize);
				item.setHeight(Config.tileSize);
				item.setGroundTileX(getTileX());
				item.setGroundTileY(getTileY());
				item.setDefId(getDef().itemId);
				item.setItemCount(1);
				//item.item = new Item(World.getNextId(),
				//		DefManager.allItems.get(getDef().itemId), null);
				//item.item.setCount(1);
				//item.setName(item.item.getName());
				item.setYacceleration(-1f);
				World.getInstance().addObject(item.getObjectId(), item);
			}
			// if (getResourceImage() != null) {
			if (getDef().drop != null) {
				for (Item it : Util.getDrop(getDef().drop)) {
					ItemObject item = new ItemObject();
					item.setWidth(Config.tileSize);
					item.setHeight(Config.tileSize);
					item.setGroundTileX(getTileX());
					item.setGroundTileY(getTileY());
					item.setY(getY());
					item.setX(getX());
					item.setDefId(it.getId());
					item.setItemCount(it.getCount());
					World.getInstance().addObject(item.getObjectId(), item);
				}
			}

		}
		HitInfo info = new HitInfo();
		info.attackoId = getX();
		info.targetOId = getY();
		info.damage = damage;
		info.isCrit = HitInfo.BLOCK;
		Connections.broadcast(info);
	}

	public void setId(short id) {
		this.id = id;
	}

	public short getId() {
		return id;
	}

	public int getMaxHp() {
		return maxHp;
	}

	public void setMaxHp(int maxHp) {
		hp = maxHp;
		this.maxHp = maxHp;
	}

	@Override
	public String getImage() {
		return getDef().image;
	}

	@Override
	public String getName() {
		return getDef().getStringProperty("name");
	}

	@Override
	public float getMass() {
		return getDef().mass;
	}

	public GroundDef getDef() {
		return DefManager.groundDefs.get((int) getId());
	}

	@Override
	protected void applyChange(AbstractMapObject mapObject) {

	}

	@Override
	public void objectChanged(GObject object) {

	}

	public short getBackgroundId() {
		return backgroundId;
	}

	public void setBackgroundId(short backgroundId) {
		this.backgroundId = backgroundId;
	}

	@Override
	public void sendTileInfo() {
		getMap().send(getTileInfo(), true);
		// Connections.broadcast(getTileInfo());
	}

	@Override
	public TileInfo getTileInfo() {
		TileInfo info = new TileInfo(getX() / Config.tileSize, getY()
				/ Config.tileSize, TYPE_GROUND);
		info.mass = getMass();
		info.hp = getHp();
		info.id = getId();
		return info;
	}

	@Override
	public void setMass(float mass) {

	}

}
